package com.definity.toolkit.report;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

import com.definity.toolkit.domain.DynamicDomain;

public class ReportUtils {

	private ReportUtils() {
		
	}
	
	public static synchronized <T extends Serializable> void openReport(String reportName, Map<String, Object> parameters, List<DynamicDomain<T>> list) {
		JasperReport jasperReport;
		JasperPrint jasperPrint;
		
		try {
			jasperReport = JasperCompileManager.compileReport(ReportUtils.class.getClassLoader().getResourceAsStream("reports/" + reportName));
			jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new DynamicDomainJRDataSource<T>(list));

			JasperViewer.viewReport(jasperPrint, false);
		} catch (JRException e) {
			throw new ReportException(e);
		}
	}
	
}
